package uicomponents;

import java.io.IOException;
import javax.faces.component.UIComponentBase;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;

public class UIGoogleMap extends UIComponentBase {
    @Override
    public void encodeEnd(FacesContext context) throws IOException
    {
        String id = (String)getAttributes().get("id");
        String width = (String)getAttributes().get("width");
        String height = (String)getAttributes().get("height");

        ResponseWriter writer = context.getResponseWriter();
        writer.startElement("div", this);
        writer.writeAttribute("id", id, null);
        writer.writeAttribute("style", "width:"+width+";height:"+height+";background:gray", null);
        writer.endElement("div");
        writer.startElement("script", this);
        writer.writeAttribute("type", "text/javascript", null);
        String options = "{zoom: 10, center: new google.maps.LatLng(50.05, 19.95), mapTypeId: google.maps.MapTypeId.ROADMAP}";
        String script = id+" = new google.maps.Map($('"+id+"'), "+options+")";
        writer.writeText(script, null);
        writer.endElement("script");

        context.getAttributes().put("mapId", id);
    }

    @Override
    public String getFamily() {
        return "Rescriptus";
    }
}
